home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’87 / Source ƒ.sit / Source ƒ / XLISP ƒ / XLISP 1.7 C SRCS / xldbug.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-07-03  |  4.0 KB  |  189 lines

  1. /* xldebug - xlisp debugging support */
  2. /*    Copyright (c) 1985, by David Michael Betz
  3.     All Rights Reserved
  4.     Permission is granted for unrestricted non-commercial use    */
  5.  
  6. #include "xlisp.h"
  7.  
  8. /* external variables */
  9. extern long total;
  10. extern int xldebug;
  11. extern int xltrace;
  12. extern int xlsample;
  13. extern NODE *s_unbound;
  14. extern NODE *s_stdin,*s_stdout;
  15. extern NODE *s_tracenable,*s_tlimit,*s_breakenable;
  16. extern NODE *true;
  17. extern NODE **trace_stack;
  18. extern char buf[];
  19.  
  20. /* external routines */
  21. extern char *malloc();
  22.  
  23. /* forward declarations */
  24. FORWARD NODE *stacktop();
  25.  
  26. /* xlfail - xlisp error handler */
  27. xlfail(emsg)
  28.   char *emsg;
  29. {
  30.     xlerror(emsg,stacktop());
  31. }
  32.  
  33. /* xlabort - xlisp serious error handler */
  34. xlabort(emsg)
  35.   char *emsg;
  36. {
  37.     xlsignal(emsg,s_unbound);
  38. }
  39.  
  40. /* xlbreak - enter a break loop */
  41. xlbreak(emsg,arg)
  42.   char *emsg; NODE *arg;
  43. {
  44.     breakloop("break",NULL,emsg,arg,TRUE);
  45. }
  46.  
  47. /* xlerror - handle a fatal error */
  48. xlerror(emsg,arg)
  49.   char *emsg; NODE *arg;
  50. {
  51.     doerror(NULL,emsg,arg,FALSE);
  52. }
  53.  
  54. /* xlcerror - handle a recoverable error */
  55. xlcerror(cmsg,emsg,arg)
  56.   char *cmsg,*emsg; NODE *arg;
  57. {
  58.     doerror(cmsg,emsg,arg,TRUE);
  59. }
  60.  
  61. /* xlerrprint - print an error message */
  62. xlerrprint(hdr,cmsg,emsg,arg)
  63.   char *hdr,*cmsg,*emsg; NODE *arg;
  64. {
  65.     sprintf(buf,"%s: %s",hdr,emsg); stdputstr(buf);
  66.     if (arg != s_unbound) { stdputstr(" - "); stdprint(arg); }
  67.     else xlterpri(getvalue(s_stdout));
  68.     if (cmsg) { sprintf(buf,"if continued: %s\n",cmsg); stdputstr(buf); }
  69. }
  70.  
  71. /* doerror - handle xlisp errors */
  72. LOCAL doerror(cmsg,emsg,arg,cflag)
  73.   char *cmsg,*emsg; NODE *arg; int cflag;
  74. {
  75.     /* make sure the break loop is enabled */
  76.     if (getvalue(s_breakenable) == NIL)
  77.     xlsignal(emsg,arg);
  78.  
  79.     /* call the debug read-eval-print loop */
  80.     breakloop("error",cmsg,emsg,arg,cflag);
  81. }
  82.  
  83. /* breakloop - the debug read-eval-print loop */
  84. LOCAL int breakloop(hdr,cmsg,emsg,arg,cflag)
  85.   char *hdr,*cmsg,*emsg; NODE *arg; int cflag;
  86. {
  87.     NODE ***oldstk,*expr,*val;
  88.     CONTEXT cntxt;
  89.     int type;
  90.  
  91.     /* print the error message */
  92.     xlerrprint(hdr,cmsg,emsg,arg);
  93.  
  94.     /* flush the input buffer */
  95.     xlflush();
  96.  
  97.     /* do the back trace */
  98.     if (getvalue(s_tracenable)) {
  99.     val = getvalue(s_tlimit);
  100.     xlbaktrace(fixp(val) ? (int)getfixnum(val) : -1);
  101.     }
  102.  
  103.     /* create a new stack frame */
  104.     oldstk = xlstack;
  105.     xlsave1(expr);
  106.  
  107.     /* increment the debug level */
  108.     xldebug++;
  109.  
  110.     /* debug command processing loop */
  111.     xlbegin(&cntxt,CF_ERROR|CF_CLEANUP|CF_CONTINUE,true);
  112.     for (type = 0; type == 0; ) {
  113.  
  114.     /* setup the continue trap */
  115.     if (type = setjmp(cntxt.c_jmpbuf))
  116.         switch (type) {
  117.         case CF_ERROR:
  118.             xlflush();
  119.             type = 0;
  120.             continue;
  121.         case CF_CLEANUP:
  122.             continue;
  123.         case CF_CONTINUE:
  124.             if (cflag) {
  125.             stdputstr("[ continue from break loop ]\n");
  126.             continue;
  127.             }
  128.             else xlabort("this error can't be continued");
  129.         }
  130.  
  131.     /* read an expression and check for eof */
  132.     if (!xlread(getvalue(s_stdin),&expr,FALSE)) {
  133.         type = CF_CLEANUP;
  134.         break;
  135.     }
  136.  
  137.     /* evaluate the expression */
  138.     expr = xleval(expr);
  139.  
  140.     /* print it */
  141.     xlprint(getvalue(s_stdout),expr,TRUE);
  142.     xlterpri(getvalue(s_stdout));
  143.     }
  144.     xlend(&cntxt);
  145.  
  146.     /* decrement the debug level */
  147.     xldebug--;
  148.  
  149.     /* restore the previous stack frame */
  150.     xlstack = oldstk;
  151.  
  152.     /* check for aborting to the previous level */
  153.     if (type == CF_CLEANUP) {
  154.     stdputstr("[ abort to previous level ]\n");
  155.     xlsignal(NULL,NIL);
  156.     }
  157. }
  158.  
  159. /* stacktop - return the top node on the stack */
  160. LOCAL NODE *stacktop()
  161. {
  162.     return (xltrace >= 0 && xltrace < TDEPTH ? trace_stack[xltrace] : s_unbound);
  163. }
  164.  
  165. /* baktrace - do a back trace */
  166. xlbaktrace(n)
  167.   int n;
  168. {
  169.     int i;
  170.  
  171.     for (i = xltrace; (n < 0 || n--) && i >= 0; i--)
  172.     if (i < TDEPTH)
  173.         stdprint(trace_stack[i]);
  174. }
  175.  
  176. /* xldinit - debug initialization routine */
  177. xldinit()
  178. {
  179.     if ((trace_stack = (NODE **)malloc(TDEPTH * sizeof(NODE *))) == NULL) {
  180.     printf("insufficient memory");
  181.     wrapup();
  182.     }
  183.     total += (long)(TDEPTH * sizeof(NODE *));
  184.     xlsample = 0;
  185.     xltrace = -1;
  186.     xldebug = 0;
  187. }
  188.  
  189.